home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / scale_geom / scaleg_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  6.9 KB  |  272 lines

  1. /*
  2.  * scaleg_main: main program for image scaling
  3.  *
  4.  * This program is a hack from the 'zoom' program by Paul Heckbert, UC Berkeley
  5.  *  The zoom program was modified to work with the Hips file format.
  6.  *  Since Hips file are not color, most (but not all) of the code for
  7.  *  handling color has been thrown out. Input and output is alway
  8.  *  stdin and stdout.
  9.  *        -Brian Tierney, LBL  9/89
  10.  *
  11.  *   terminology:
  12.  *       x = colums
  13.  *       y = rows
  14.  *
  15.  * see additional comments in scale_g.c and pic.3
  16.  *
  17.  */
  18.  
  19. #include <math.h>
  20.  
  21. #include "hips.h"
  22. #include "simple.h"
  23. #include "pic.h"
  24. #include "filt.h"
  25. #include "scaleg.h"
  26.  
  27. #define FILTER_DEFAULT "triangle"
  28. #define WINDOW_DEFAULT "blackman"
  29.  
  30. double    atof_check();
  31.  
  32. #include "usage.h"
  33.  
  34.     struct header hd;        /* global hips header */
  35.  
  36. main(ac, av)
  37.     int       ac;
  38.     char    **av;
  39. {
  40.     char     *xfiltname = FILTER_DEFAULT, *yfiltname = 0;
  41.     char     *xwindowname = 0, *ywindowname = 0;
  42.     int       xyflag, yxflag, doubleflag, tripleflag;
  43.     int       nocoerce, nchan, square, intscale, keepzeros, i;
  44.     double    xsupp = -1., ysupp = -1.;
  45.     double    xblur = -1., yblur = -1.;
  46.     Pic      *apic, *bpic;
  47.     Window_box a;        /* src window */
  48.     Window_box b;        /* dest window */
  49.  
  50.     Mapping   m;
  51.     Filt     *xfilt, *yfilt, xf, yf;
  52.  
  53.     Progname = strsave(*av);
  54.     a.x0 = a.y0 = a.x1 = a.y1 = a.nx = a.ny = 0;
  55.     b.x0 = b.y0 = b.x1 = b.y1 = b.nx = b.ny = 0;
  56.     m.sx = 0.;
  57.     square = 0;
  58.     intscale = 0;
  59.     xyflag = 0;
  60.     yxflag = 0;
  61.     doubleflag = tripleflag = 0;
  62.     nocoerce = 0;
  63.     keepzeros = 0;
  64.  
  65.     for (i = 1; i < ac; i++)
  66.     if (av[i][0] == '-')
  67.         if (str_eq(av[i], "-s") && ok(i + 4 < ac, "-s")) {
  68.         a.x0 = atof_check(av[++i]);
  69.         a.y0 = atof_check(av[++i]);
  70.         a.nx = atof_check(av[++i]);
  71.         a.ny = atof_check(av[++i]);
  72.         } else if (str_eq(av[i], "-d") && ok(i + 2 < ac, "-d")) {
  73.         b.nx = atof_check(av[++i]);
  74.         b.ny = atof_check(av[++i]);
  75.         } else if (str_eq(av[i], "-dc"))
  76.         b.nx = atof_check(av[++i]);
  77.         else if (str_eq(av[i], "-dr"))
  78.         b.ny = atof_check(av[++i]);
  79.         else if (str_eq(av[i], "-intscale"))
  80.         intscale = 1;
  81.         else if (str_eq(av[i], "-filt") && ok(i + 1 < ac, "-filt")) {
  82.         xfiltname = av[++i];
  83.         if (i + 1 < ac && av[i + 1][0] != '-')
  84.             yfiltname = av[++i];
  85.         } else if (str_eq(av[i], "-supp") && ok(i + 1 < ac, "-supp")) {
  86.         xsupp = atof_check(av[++i]);
  87.         if (i + 1 < ac && av[i + 1][0] != '-')
  88.             ysupp = atof_check(av[++i]);
  89.         } else if (str_eq(av[i], "-blur") && ok(i + 1 < ac, "-blur")) {
  90.         xblur = atof_check(av[++i]);
  91.         if (i + 1 < ac && av[i + 1][0] != '-')
  92.             yblur = atof_check(av[++i]);
  93.         } else if (str_eq(av[i], "-window") && ok(i + 1 < ac, "-window")) {
  94.         xwindowname = av[++i];
  95.         if (i + 1 < ac && av[i + 1][0] != '-')
  96.             ywindowname = av[++i];
  97.         } else if (str_eq(av[i], "-debug") && ok(i + 1 < ac, "-debug"))
  98.         zoom_debug = atof_check(av[++i]);
  99.         else if (str_eq(av[i], "-xy"))
  100.         xyflag = 1;
  101.         else if (str_eq(av[i], "-yx"))
  102.         yxflag = 1;
  103.         else if (str_eq(av[i], "-plain"))
  104.         nocoerce = 1;
  105.         else if (str_eq(av[i], "-keep0"))
  106.         keepzeros = 1;
  107.         else if (str_eq(av[i], "-dev")) {
  108.         pic_catalog();
  109.         exit(0);
  110.         } else if (str_eq(av[i], "-2")) {
  111.         doubleflag = 1;
  112.         } else if (str_eq(av[i], "-3")) {
  113.         tripleflag = 1;
  114.         } else if (str_eq(av[i], "-h")) {
  115.         fputs(usage, stderr);
  116.         exit(0);
  117.         } else {
  118.         if (!str_eq(av[i], "-"))
  119.             fprintf(stderr, "unrecognized argument: %s\n", av[i]);
  120.         fputs(usage, stderr);
  121.         exit(1);
  122.         }
  123.  
  124.     if (str_eq(xfiltname, "?")) {
  125.     filt_catalog();
  126.     exit(0);
  127.     }
  128.     if (xyflag)
  129.     zoom_xy = 1;
  130.     if (yxflag)
  131.     zoom_xy = 0;
  132.     zoom_coerce = !nocoerce;
  133.     zoom_trimzeros = !keepzeros;
  134.  
  135.     /* for hips files */
  136.     apic = pic_open("stdin", "r");
  137.     bpic = pic_open("stdout", "w");
  138.  
  139.     nchan = 1;            /* hips stuff is always 1 channel */
  140.     /* check input image size */
  141.     if (a.x0 > hd.ocols || a.y0 > hd.orows || a.nx > hd.ocols || a.ny > hd.orows) {
  142.     fprintf(stderr, "\n Error: input image is not that big. \n\n");
  143.     exit(-1);
  144.     }
  145.     /*
  146.      * set defaults
  147.      */
  148.     if (a.nx == 0)
  149.     a.nx = hd.ocols;
  150.     if (a.ny == 0)
  151.     a.ny = hd.orows;
  152.     /* want square pixels, so scale x or y to the appr. value */
  153.     if (b.nx == 0 && b.ny == 0) {
  154.     b.nx = a.nx;
  155.     b.ny = a.ny;
  156.     }
  157.     if (b.nx == 0)
  158.     b.nx = (int) ((b.ny * a.nx) / (float) a.ny + .5);
  159.     if (b.ny == 0)
  160.     b.ny = (int) ((b.nx * a.ny) / (float) a.nx + .5);
  161.  
  162.     if (doubleflag) {
  163.     b.nx = a.nx * 2;
  164.     b.ny = a.ny * 2;
  165.     }
  166.     if (tripleflag) {
  167.     b.nx = a.nx * 3;
  168.     b.ny = a.ny * 3;
  169.     }
  170.     if (b.nx > XMAX || b.ny > YMAX) {
  171.     fprintf(stderr, "\nError: destination file must be smaller than %d by %d \n\n",
  172.         XMAX, YMAX);
  173.     exit(-1);
  174.     }
  175.  
  176.     hd.ocols = hd.cols = b.nx;
  177.     hd.orows = hd.rows = b.ny;        /* must do this after call to zoom */
  178.     update_header(&hd, ac, av);
  179.     write_header(&hd);
  180.  
  181.     /* sets x1 and y1 values */
  182.     window_box_set_max(&a);
  183.     window_box_set_max(&b);
  184.  
  185.     if (!yfiltname)
  186.     yfiltname = xfiltname;
  187.     xfilt = filt_find(xfiltname);
  188.     yfilt = filt_find(yfiltname);
  189.     if (!xfilt || !yfilt) {
  190.     fprintf(stderr, "can't find filters %s and %s\n",
  191.         xfiltname, yfiltname);
  192.     exit(1);
  193.     }
  194.     /* copy the filters before modifying them */
  195.     xf = *xfilt;
  196.     xfilt = &xf;
  197.     yf = *yfilt;
  198.     yfilt = &yf;
  199.     if (xsupp >= 0.)
  200.     xfilt->supp = xsupp;
  201.     if (xsupp >= 0. && ysupp < 0.)
  202.     ysupp = xsupp;
  203.     if (ysupp >= 0.)
  204.     yfilt->supp = ysupp;
  205.     if (xblur >= 0.)
  206.     xfilt->blur = xblur;
  207.     if (xblur >= 0. && yblur < 0.)
  208.     yblur = xblur;
  209.     if (yblur >= 0.)
  210.     yfilt->blur = yblur;
  211.  
  212.     if (!ywindowname)
  213.     ywindowname = xwindowname;
  214.     if (xwindowname || xfilt->windowme) {
  215.     if (!xwindowname)
  216.         xwindowname = WINDOW_DEFAULT;
  217.     xfilt = filt_window(xfilt, xwindowname);
  218.     }
  219.     if (ywindowname || yfilt->windowme) {
  220.     if (!ywindowname)
  221.         ywindowname = WINDOW_DEFAULT;
  222.     yfilt = filt_window(yfilt, ywindowname);
  223.     }
  224.     if (xfilt->printproc) {
  225.     fprintf(stderr, "xfilt: ");
  226.     filt_print_client(xfilt);
  227.     }
  228.     if (yfilt->printproc) {
  229.     fprintf(stderr, "yfilt: ");
  230.     filt_print_client(yfilt);
  231.     }
  232.     /* process each of the frames */
  233.     for (i = 0; i < hd.num_frame; i++) {
  234.     if (hd.num_frame > 1)
  235.         fprintf(stderr, "\n processsing frame %d...", i);
  236.     load_hips_frame(apic->data);
  237.     zoom_opt(apic, &a, bpic, &b, xfilt, yfilt, square, intscale);
  238.     store_hips_frame(bpic->data);
  239.     }
  240.     fprintf(stderr, "\n\n");
  241.     return (0);
  242. }
  243.  
  244. /************************************************/
  245. ok(enough, option)
  246.     int       enough;
  247.     char     *option;
  248. {
  249.     if (!enough) {
  250.     fprintf(stderr, "insufficient args to %s\n", option);
  251.     exit(1);
  252.     }
  253.     return 1;
  254. }
  255.  
  256. /************************************************/
  257. /* atof_check: ascii to float conversion with checking */
  258.  
  259. double
  260. atof_check(str)
  261.     char     *str;
  262. {
  263.     char     *s;
  264.  
  265.     for (s = str; *s; s++)
  266.     if (strchr("0123456789.+-eE", *s) == NULL) {
  267.         fprintf(stderr, "expected numeric argument, not %s\n", str);
  268.         exit(1);
  269.     }
  270.     return atof(str);
  271. }
  272.